package yl.hs.bmipfull.service.laboratorydata.imple;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.laboratorydata.ILabRmInspectionRecordDao;
import yl.hs.bmipfull.daos.laboratorydata.ILabSandInspectionItemsDao;
import yl.hs.bmipfull.pojo.laboratorydata.*;
import yl.hs.bmipfull.service.laboratorydata.ILabSandInspectionItemsService;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author 吴高丰
 * @apiNote 服务层接口实现
 * @since 2022年06月24日 09时06分58秒 <br/>
 */
@Service
public class LabSandInspectionItemsServiceImpl extends LaboratorydataBaseService implements ILabSandInspectionItemsService {

    @Autowired
    ILabSandInspectionItemsDao dao;
    @Autowired
    ILabRmInspectionRecordDao recordDao;
    @Override
    public List<LabSandInspectionItemsEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(LabSandInspectionItemsEntity.class);
        pageQuery.getQuery().put("sortKey", String.join(",", pageQuery.getSortSides()));
        pageQuery.getQuery().put("unitSNArray", getCompanySNList());
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), LabSandInspectionItemsEntity.class);
        return dao.queryPage(pageQuery.getQuery(),null);
    }

    @Override
    public PageResult<LabSandInspectionItemsEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(LabSandInspectionItemsEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<LabSandInspectionItemsEntity> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), LabSandInspectionItemsEntity.class);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(),filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(),filters));
        return result;
    }
    @Override
    public Map<String,Object> findBySn(String sn) {
        var LabRmInspectionRecordEntity = recordDao.findBySN(sn);
        var LabSandInspectionItemsEntity =  dao.queryBySN(sn);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("LabRmInspectionRecordEntity",LabRmInspectionRecordEntity);
        map.put("LabSandInspectionItemsEntity",LabSandInspectionItemsEntity);
        return map;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveLabSandInspectionItems(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        LabRmInspectionRecordEntity entity = null;
        ObjectMapper objectMapper = new ObjectMapper();
        if (Tl.isEmpty(sn)) {
            //var LabRmInspectionRecordEntityList = (List<LabRmInspectionRecordEntity>)model.get("lab_rm_inspection_record");
            //执行保存
            entity = new LabRmInspectionRecordEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(LabRmInspectionRecordEntity.class));
            var results = recordDao.insertNew(entity);
            if (results > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入信息", Tl.toJson(entity), LabRmInspectionRecordEntity.class);
            }
            var subData=model.get("subData");
            var subDataEntity=objectMapper.convertValue(subData, LabSandInspectionItemsEntity.class);
            subDataEntity.setInspectionSN(entity.getSn());
            result =dao.insertNew(subDataEntity);
                if (result > 0) {
                    writeLog(ActionType.CREATE, subDataEntity.getInspectionSN(), "插入信息", Tl.toJson(subDataEntity), LabSandInspectionItemsEntity.class);
                }
        } else {
            //执行修改
            entity = recordDao.findBySN(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = recordDao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新信息", Tl.toJson(entity), LabSandInspectionItemsEntity.class);
            }
            var  delenumber =  delete(sn);
            var subData=model.get("subData");
            var subDataEntity=objectMapper.convertValue(subData, LabSandInspectionItemsEntity.class);
            subDataEntity.setInspectionSN(entity.getSn());
            result =dao.insertNew(subDataEntity);
            if (result > 0) {
                writeLog(ActionType.CREATE, subDataEntity.getInspectionSN(), "插入信息", Tl.toJson(subDataEntity), LabSandInspectionItemsEntity.class);
            }
        }
        return result;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String sn) {
        Integer result = dao.DeleteByInspectionSN(sn);
        return result;
    }

    @Override
    public List<LabSandInspectionItemsEntity> queryBySN(String inspectionSN) {
        return dao.queryBySN(inspectionSN);
    }
}